`
https://leetcode.cn/problems/non-decreasing-subsequences/
`

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var findSubsequences = function (nums) {
  const res = []
  // 记录回溯的路径
  const track = []

  const backtrack = (start) => {
    // 找到一个合法答案
    if (track.length >= 2) {
      res.push(track.slice())
    }

    // 用集合保证每层只会用到一次相同的数字，剪枝
    const used = new Set()

    for (let i = start; i < nums.length; i++) {
      // 保证集合中元素都是递增顺序
      if (track.length !== 0 && track[track.length - 1] > nums[i]) {
        continue;
      }
      // 在这一层里用过了就不需要再用了
      if (used.has(nums[i])) continue

      track.push(nums[i])
      // 记录已使用
      used.add(nums[i])
      // 递归遍历下一层回溯树
      backtrack(i + 1)
      track.pop()
    }
  }

  backtrack(0)
  return res
};

// test
console.log(findSubsequences([4, 6, 7, 7])) // [[4,6],[4,7],[4,7,7],[6,7],[6,7],[7,7]]
console.log(findSubsequences([4, 4, 3, 2, 1])) // [[4,4]];
